﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Utils;
using Jungle.Code.DAL;

namespace Jungle.Code.Core
{
    public class SessionKeeper
    {
        // Fields
        private string sessionId;
        private int userId;
        private string userLogin;
        private string adminLogin;

        //Properties
        public string SessionId
        {
            get { return sessionId; }
            set { sessionId = value; }
        }
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        public string UserLogin
        {
            get { return userLogin; }
            set { userLogin = value; }
        }
        public string AdminLogin
        {
            get { return adminLogin; }
            set { adminLogin = value; }
        }

        public SessionKeeper()
        {
            CheckSessions();
        }

        public void Create()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("crSession_Create", connect);
                SqlParameter param_SessionId = command.Parameters.Add("@SessionId", SqlDbType.NChar, 24);
                SqlParameter param_UserId = command.Parameters.Add("@userId", SqlDbType.Int);
                SqlParameter param_UserLogin = command.Parameters.Add("@userLogin", SqlDbType.NChar, 255);
                SqlParameter param_AdminLogin = command.Parameters.Add("@adminLogin", SqlDbType.NChar, 255);

                command.CommandType = CommandType.StoredProcedure;
                param_SessionId.Value = sessionId;
                param_UserId.Value = userId;
                param_UserLogin.Value = userLogin;
                param_AdminLogin.Value = adminLogin;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }

        public void GetData()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("crSession_GetData", connect);
                SqlParameter param_SessionId = command.Parameters.Add("@SessionId", SqlDbType.NChar, 24);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                param_SessionId.Value = sessionId;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    userId = Convert.ToInt32(reader.GetValue(1));
                    userLogin = reader.GetValue(2).ToString().Trim();
                    adminLogin = reader.GetValue(3).ToString().Trim();
                }
                else
                {
                    userId = -1;
                }
                reader.Close();
                command.Connection.Close();
            }
        }

        private void CheckSessions()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("crSession_GetAll", connect);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TimeSpan timeSpan = new TimeSpan();
                    timeSpan = DateTime.Now - Convert.ToDateTime(reader.GetValue(4));
                    if (timeSpan.Days > 1) DeleteSession(reader.GetValue(1).ToString().Trim());
                }
                reader.Close();
                command.Connection.Close();
            }            
        }

        public void DeleteSession(string _sessionId)
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("crSession_Delete", connect);
                SqlParameter param_SessionId = command.Parameters.Add("@SessionId", SqlDbType.NChar, 24);

                command.CommandType = CommandType.StoredProcedure;
                param_SessionId.Value = _sessionId;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }            
        }
    }
}